package com.wink_172.library.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import androidx.databinding.DataBindingUtil
import com.alibaba.fastjson.JSON
import com.wink_172.library.R
import com.wink_172.library.databinding.ActivityFeedBackBinding
import com.wink_172.library.model.json.CommonRootBean
import com.wink_172.library.utils.CommonUtils
import com.wink_172.library.view.ActionBarView
import com.wink_172.library.view.CustomEditText
import org.xutils.common.Callback.CancelledException
import org.xutils.common.Callback.CommonCallback
import org.xutils.x

/**
 * 意见反馈
 */
class FeedBackActivity : SmartActivity() {
    lateinit var binding: ActivityFeedBackBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStatusBarLightMode()
        binding = DataBindingUtil.setContentView(this, R.layout.activity_feed_back)

        initView()
        init()
    }

    override fun onClick(v: View) {
        super.onClick(v)
        if (v.id == R.id.btn_center) {
            feedback()
        }
    }

    fun initView() {
        (binding!!.actionBar as ActionBarView).updateAllContent(
            R.mipmap.icon_back,
            "Kotlin意见反馈",
            ""
        );

        (binding!!.actionBar as ActionBarView).setCallback (object : ActionBarView.ICallback {
            override fun onSendEvent(event: Int, content: String?) {
                if (event == ActionBarView.LEFT_BTN) {
                    finish()
                }
            }


        })
    }


    private fun init() {}

    /**
     * 意见反馈
     */
    private fun feedback() {
        val input_content = (binding!!.inputContent as CustomEditText).text.toString()
        if (TextUtils.isEmpty(input_content)) {
            CommonUtils.showToast("请输入内容")
            return
        }
        val params = CommonUtils.commonRequestParams("url")
        params.addBodyParameter("content", input_content)

//        if (Constants.TEST_MODE) {
//            Log.e(TAG, CommonUtils.getHeadSuffix() + ": ====>>" + params.toJSONString());
//        }
        val commonCallback: CommonCallback<*> = object : CommonCallback<String> {
            override fun onSuccess(result: String) {
                Log.e(TAG, CommonUtils.headSuffix + ": ====>>" + result)
                val commonRootBean = JSON.parseObject(result, CommonRootBean::class.java)
                if (commonRootBean.code == 1) {
                    CommonUtils.showToast("感谢反馈！！")
                    finish()
                } else {
                    CommonUtils.showToast(commonRootBean.message)
                }
            }

            override fun onError(ex: Throwable, isOnCallback: Boolean) {
                Log.e(TAG, "onError: ====>>$ex")
                CommonUtils.showToast(ex.toString())
            }

            override fun onCancelled(cex: CancelledException) {}
            override fun onFinished() {
                hideProgressDialog()
            }
        }
        x.http().post(params, commonCallback)
    }

    companion object {
        fun startActivity(context: Context, vararg args: Any?) {
            val intent = Intent(context, FeedBackActivity::class.java).apply {
                //    putExtra(Constants.PARAM_DATA1, (String) args[0]);
            }


            context.startActivity(intent)
        }

        private const val TAG = "FeedBackActivity"
    }
}

